package com.bestcem.xm.common.core.config.log;

import ch.qos.logback.classic.net.SMTPAppender;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.helpers.CyclicBuffer;

/**
 * @description:
 * @author: zhenz
 * @date: 2020/3/20 13:26
 */
public class LimitSmtpAppender extends SMTPAppender {

    /**
     * 获取最新的50条记录
     */
    private int limit = 50;

    @Override
    protected void fillBuffer(CyclicBuffer<ILoggingEvent> cb, StringBuffer sbuf) {
        int len = cb.length();
        int start = 0;

        // 缓冲区里小于50条
        if (limit < len) {
            start = len - limit - 1;
        }

        for (int i = start; i < len; i++) {
            ILoggingEvent event = cb.get(i);
            sbuf.append(layout.doLayout(event));
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
}
